home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Commodore Free 7
/
Commodore_Free_Issue_07_2007_Commodore_Computer_Club.d64
/
t.loadstar prg 2
< prev
next >
Wrap
Text File
|
2023-02-26
|
14KB
|
589 lines
uProgramming II
By Dave Moorman
THE SHELL
When you have played with your program
long enough -- and have some idea how
variables and PRINT work -- type:
NEW
We are going to now prepare a disk or
disk image & save a program. As you
certainly know, when you turn off a
computer, anything in memory will
disappear -- lost forever. To save our
work, we have to SAVE our work. [If you
are learning how to program from the
VICE emulator on NICKEL GAMES, you
won't have to do this until later. Your
emulator is already "attached" to a
disk image called MYDISK.D81."]
PREPARING A DISK -- REAL C-64 Connect
the drive to the computer & turn both
on. Type (carefully & exactly)
OPEN1,8,15,"N0:MYDISK,01":CLOSE1
And press <RETURN>. When the cursor
reappears, Type:
LOAD"$",8
Then LIST.
This will show your disk directory. It
also erases whatever program is in
memory.
PREPARING A DISK IMAGE -- VICE
Press <Alt-8>.
Navigate to the folder where you want
to keep your programs. You can create
a new folder if you want -- just like
with any Windows program.Type in the
filename textbox:
mydisk
but do not press <ENTER> yet. Instead,
look down at the lower left of the
dialog box. You will see the word VICE
in a textbox. Change it to
mydisk
Also change the ID to 01. Another box
shows D64. This will be fine for now.
Click on CREATE IMAGE. Then click OPEN.
You have just created a disk image &
attached it to VICE.
NOTE:
Every disk or disk image should have a
unique ID. We just used 01 in the above
examples. Whenever you format a real
disk or make a disk image, give it a
different ID, which can be any 2
alphanumeric characters. When you
launch VICE (other than with NICKEL
GAMES), you will need to "attach" a
disk image to it. This is just like
putting a real disk in a real drive.
Press <Alt-8>, navigate to the disk
image you want (it will have a .D64 or
.D81 extension) & click OPEN.
Getting a disk directory in VICE is the
same as with a real C-64:
LOAD"$",8
LIST
Now you are ready to create a Scratch &
Save Shell. NEW your memory & type:
59999 END
60000 D = PEEK(186): IF D<8THEN D=8
60001 OPEN1,D,15,"I0":N$="SHELL.RTN"
60002 PRINT#1,"S0:"+N$:CLOSE1
60003 SAVE N$,D
60004 VERIFY N$,D: END
Be sure you have typed everything
correctly. You MUST type out all the
letters in "PRINT#1," on line 60002!
Then Type:
GOTO60000
When you are successful (IE, no
errors), Type:
LOAD"$",8
LIST
You should see
0 "MYDISK " 01 2A
1 "SHELL.RTN" PRG
663 BLOCKS FREE.
(With VICE, the disk label ("MYDISK")
will look strange unless you typed in
all capital letters in the textbox in
the lower left of the dialog box. Press
<CTRL-Shift> to make uppercase lower, &
the strange characters upper case.)
Now, whenever you begin a new program,
load your Shell Routine with:
LOAD"SHELL.RTN",8
then
LIST 60001
And change "SHELL.RTN" to whatever you
are going to call your new program. For
example:
60001 OPEN1,D,15,"I0": N$="MY FIRST"
Save your new program with
GOTO60000
While you are working on your program,
take a moment every so often to type
GOTO60000 in Immediate Mode. Your
latest version will be saved to your
disk. This technique guarantees that
you will always save your program to
the correct filename. Whatever name you
LOAD will be the name that is scratched
& saved. We won't go into all the
particulars of this code, except to
mention three things. First, you can
put more than one command on a line. In
fact, a program line can be up to 2
screen lines long.
Second, notice how the numeric variable
D is used. It is set to the last used
device number -- usually the drive from
which you loaded the program -- which
the computer keeps in location 186.
Disks are always device number 8 or
higher. By using D, we don't have to
think about which device we are using,
& the same value will be used every
time. Third, by making N$ hold the name
of the program, we know that the
correct filename will be scratched
(S0:) & saved. This is really advanced
stuff -- but it serves any programmer
so well, I wanted to get you started
off right. I learned to do this after a
tragic experience.
I was working on two programs that
worked together. "B.PROG" set things up
in the computer (called a BOOT), then
loaded & ran "PROG" (the main program).
All went fine until I accidentally
saved "B.PROG" to the file name of
"PROG". Suddenly, I had two files with
the Boot program, & lost the main
program (and some 30 hours of work)
completely. Salvation is not just a
theological nicety when it comes to
computers.
Check out John 3:16 to see what
happens to "bad little programs!"
(NOTE: They "perish!") So I started
using this scratch & save routine with
every program. No matter what the name
is, it is saved to the correct file-
name. I learned something else. That
main program was getting clunky, with
lots of fixes & fixes of fixes. When I
lost it, I was about two-thirds through
it. By then, I really knew what I
needed to do. So recreating it took
only a few hours. The result was a much
better, faster, more elegant program.
Sometimes when a program becomes too
ungainly, I clench my teeth & delete
it! It means rewriting everything. But
often, that is a GOOD THING.
LOOPING
Computers are great at doing things
over & over again. They accomplish
this with a LOOP. The program that
waits for keystrokes is a loop, asking
"Has a key been pressed?" over & over.
Here is an Infinite LOOP:
10 GOTO 10
When you run this, nothing will happen!
The program keeps jumping to line 10,
over & over. On a PC, this would be
called a "lock-up!" But you have the
<STOP> key (VICE: <ESC>). Press it, &
you will break out of the loop. For a
more useful (& interesting) loop, try
this:
10 X = 0
20 X = X + 1
30 ? X;
40 GOTO 20
Here X is set to 0, then the contents
of X have 1 added, the result put back
into X. This is called "incrementing."
Line 30 prints X, & line 40 jumps
back to line 20. X is incremented &
printed, then the program loops again.
Again, this is an infinite loop. The
only way to stop it is to press <STOP>.
What we need is to stop the program
when a certain "condition" is true.
Hey! we have a Conditional Jump!
10 X = 0
20 X = X + 1
30 ? X;
40 IF X<100 THEN 20
50 END
Here, line 40 asks a conditional
question. Is the value in X less than
100? If so, (THEN) we jump back to
line 20. If not, we "fall through." We
have several conditional operators that
compare two values (or strings!).
X < A X less than A
X > A X greater than A
X <= A X less than or equal to A
X => A X equal to or greater than A
X = A X equal to A
X <> A X not equal to A
You might want to play around with the
above program until you are familiar
with how all these comparisons work.
Here is an example of two nested loops:
10 Y = 0
11 X = 0
20 ? X; Y
30 IF X < 5 THEN X = X + 1:GOTO 20
40 IF Y < 4 THEN Y = Y + 1:GOTO 11
50 END
Try to figure out what this will do
before running it. One of the essential
skills of a programmer is to be able to
read code exactly the way the computer
will. You might want to "desk check"
this program.
Write on a piece of paper:
X Y
--- ---
0 0
Then, step through the program,
changing the values as the computer
would:
X Y
--- ---
0 0
1 0
2 0
etc.
FOR-NEXT LOOPS
Conditional jumps work just fine, but
we have a better way to do counted
loops. If you want to count from 1 to
10, you can use:
10 FOR X = 1 TO 10
20 ? X;
30 NEXT
40 END
X is set to 1 & when the computer
encounters NEXT, X is incremented.
When X is greater than 10, the NEXT
"falls through" to the next line.
We can count by values other than 1.
10 FOR X = 1 TO 100 STEP 5: ? X;
20 NEXT: END
We can count backwards.
10 FOR X = 100 TO 0 STEP - 12: ?X;
20 NEXT: END
We can even nest FOR-NEXT loops.
10 :FOR X = 0 TO 5
20 : FOR Y = 0 TO 5
30 : ? X*6+Y;
40 : NEXT
50 :?
60 :NEXT
70 END
(I indented the inner loop so it's
easier to read.) One important thing to
remember whenever using FOR-NEXT Loops:
Always, ALWAYS exit the loop through
the NEXT command. ALWAYS! Whenever a
FOR command is encountered, information
is stuffed away in a special place in
memory called the Stack. If you do
something like this:
10 FOR X = 1 TO 10
20 IF X = 5 THEN 40
30 NEXT
40 ? X
50 END
You have jumped out of the loop
illegally. The stuff on the Stack is
not removed, & you can get OUT OF
MEMORY ERRORs or other strange
problems. If you need to jump out of a
FOR-NEXT loop, use code like this:
10 Y = 0: FOR X = 1 TO 10
20 IF X = 5 THEN Y = X: X = 100
30 NEXT
40 ? X, Y
50 END
The "found" value will be in Y. If Y is
0, then the value was not found. This
will become more important later. Try
this routine using different values in
line 20 -- such as IF X = 55 THEN....
GOSUB
We have mentioned GOTO, which jumps to
a given program line number. Sometimes,
you will want to use the same code at
different times in a program. Rather
than write the code over & over, you
can write a Subroutine. This example is
too simple, but here goes...
10 FOR X = 1 TO 10
20 GOSUB 100
30 NEXT
40 GOSUB 100
50 END
60 :
100 ? X
110 RETURN
Line 100 - 110 is the subroutine. When
GOSUB 100 is encountered, the program
jumps to line 100. When the RETURN
command is encountered, the program
returns to the place where it did the
GOSUB command. Be sure to keep the
program from wandering into the
subroutine without a GOSUB.
You will get a RETURN WITHOUT GOSUB
ERROR. To see how it works, remove line
50.
ARRAYS
Just one more thing before we get to
the BASIC Bible -- our list of BASIC
commands, functions, & operators. We
have discussed how a variable is like a
little box. But what if you have
several boxes that are in some way
related to each other? We have Arrays!
If a variable is a box, an array is a
chest-of-drawers. Imagine a file
cabinet called V$ that has 3 drawers.
The drawers are numbered. We can put
string data in drawer 0, 1, or 2.
10 DIM V$(2)
This DIMensions the V$ array with 3
"elements." We always have element 0,
so DIM V$(2) has three elements.
20 V$(0) = "DAD"
21 V$(1) = "LAD"
22 V$(2) = "MOM"
30 FOR X = 0 TO 2
40 ? V$(X)" - ";
50 NEXT: ?: END
Here is another way to "load" an array:
10 DIM V$(3)
20 FOR X = 0 TO 3
30 READ V$(X)
40 NEXT
50 FOR X = 3 TO 0 STEP -1
60 ? V$(X)
70 NEXT
80 END
100 DATA "MOM","LAD"
101 DATA "SIS","DAD"
Each time READ is encountered, the next
item in the DATA statements is read
into V$(X). Each string item should be
set off with double-quote marks. Also,
note that no comma is used at the end
of line 100.
The end of the line serves as a
separator between "LAD" & "SIS". Here
is something else you can do with
arrays. Add these lines to the above:
80 Y = 0: FOR X = 0 TO 3
85 IF V$(X) = "LAD" THEN Y = X:X = 3
90 NEXT:IF Y = 0 THEN ?"NOT FOUND": END
95 ? V$(Y)" FOUND!": END
You can see why it might be important
to exit a FOR-NEXT loop legally! You
can use arrays to hold lists of
information which you can alphabetize
or search. While we are at it, here is
a simple way to sort an array.
10 DIM A$(3)
15 REM READ IN THE ARRAY
20 FOR X = 0 TO 3: READ A$(X):NEXT
25 REM NOW SORT IT
30 FOR X = 0 TO 2: LO = X: LO$ = A$(X)
40 FOR Y = X + 1 TO 3
50 IF A$(Y)<LO$ THEN LO=Y:LO$=A$(Y)
60 NEXT
70 A$(LO)=A$(X)
80 A$(X)=LO$
90 NEXT
100 FOR X = 0 TO 3: ? A$(X):NEXT: END
200 DATA "MOM","SIS","LAD","DAD"
This is not necessarily the most
efficient way to sort, but you get the
idea. Desk check this code to see how
it works. Arrays can be numeric as well
as string. Just don't use the dollar
sign! Also, arrays can be any number of
dimensions & any size -- as long as
there is enough memory. Perhaps you can
have a checkerboard with 8 elements by
8 elements:
10 DIM CB(7,7)
Or you can even do three-, four-, even
five-dimensional arrays. For example,
perhaps you want to make a database
where you have 100 Last Names, First
Names, Addresses, Cities, States, &
ZIPs -- all together in one array.
10 DIM DB(5,100)
To sort by States, you would look at
DB(4,X). Play with the idea!
C-64 SECRETS
I mentioned that BASIC 2.0 does not
have commands for C-64 features such
as screen color. But this is fairly
easily remedied. Such things are
controlled by "registers," certain
bytes in memory that connect to the
chips that do the video stuff.
BACKGROUND COLOR POKE 53281,color
BORDER COLOR POKE 53280,color
TEXT COLOR POKE 646,color
The color values are as follows:
0 BLACK 8 ORANGE
1 WHITE 9 BROWN
2 RED 10 Lt RED
3 CYAN 11 DK GRAY
4 PURPLE 12 MED GRAY
5 GREEN 13 Lt GREEN
6 BLUE 14 Lt BLUE
7 YELLOW 15 Lt GRAY
Another way to change the color of the
text is to "embed" color codes in a
string. When you type a double-quote
<Shift-2>, the edit screen goes into
"quote mode." Then you can press any
keys -- including color, cursor, home,
or clear screen, & the action will be
embedded in the string. You can try
this:
10 POKE 53280, 0
20 POKE 53281, 0
30 ?"<CTRL-2>WHITE <CTRL-3>RED ";
35 ?"<CTRL-4>CYAN"
40 ?"<C=-1>ORANGE <C=-2>BROWN ";
45 ?"<C=-3>LT. RED"
(CTRL means CONTROL. On VICE <CTRL> is
<Tab>. C= is the "chicken-lips" C= key
On VICE, <C=> is <CTRL>. That makes
sense, right?)
You have a great collection of special
graphics characters built into the C64.
On a real machine, you can see them
printed on the keys. For VICE users,
you will just have to do some
experimenting. Try this:
10 ?"<C=R> <SPACE> <C=R><SPACE> <C=-R>
20 ?"<C=-Q> <Shift-]> <C=-W><SPACE>
<Shift-Minus>
30 ?"<C=-E> <SPACE> <C=-E><SPACE>
<C=-E>
You should see HI in large letters. The
most important keys are:
<C= Q> LEFT T
<C= W> RIGHT T
<C= E> DOWN T
<C= R> UP T
<C= A> UPPER LEFT CORNER
<C= S> UPPER RIGHT CORNER
<C= Z> LOWER LEFT CORNER
<C= X> LOWER RIGHT CORNER
<Shift ]> HORIZONAL LINE
<Shift -> VERTICAL LINE
About the only letter you cannot make
with these 10 graphic characters is X.
Well, that pretty much covers the
basics of BASIC structures & controls.
Now we have a bunch of commands,
functions, & operations to learn. You
will want to play with each of these
commands to get familiar with them.
Then keep this info handy as you begin
programming. But mostly, have fun!